#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import threading
import tkinter as tk
from tkinter import filedialog, messagebox
from pathlib import Path

import gui_app_pic_sub as pipeline


def run_pipeline_async(input_path: str, output_path: str, output_path3: str, btn: tk.Button):
	def _task():
		try:
			pipeline.main(input_folder=input_path, output_root=output_path, output_root3=output_path3)
			messagebox.showinfo("Done", "Pipeline finished successfully!")
		except Exception as e:
			messagebox.showerror("Error", f"Execution failed: {e}")
		finally:
			btn.config(state=tk.NORMAL, text="Submit")
	threading.Thread(target=_task, daemon=True).start()


def create_ui():
	root = tk.Tk()
	root.title("Image Processing & Auto Labeling")
	root.geometry("920x360")

	frame = tk.Frame(root, padx=24, pady=24, highlightthickness=3, highlightbackground="#000", bd=0)
	frame.pack(fill=tk.BOTH, expand=True)

	# Input folder
	row1 = tk.Frame(frame)
	row1.pack(fill=tk.X, pady=12)
	tk.Label(row1, text="Image folder:", width=10, anchor='e').pack(side=tk.LEFT)
	input_var = tk.StringVar()
	input_entry = tk.Entry(row1, textvariable=input_var, font=("Segoe UI", 11))
	input_entry.pack(side=tk.LEFT, fill=tk.X, expand=True, ipady=8, padx=(8, 8))

	def on_browse():
		path = filedialog.askdirectory(title="Select image folder")
		if path:
			input_var.set(path)
	# Browse button
	tk.Button(row1, text="Browse", width=8, command=on_browse).pack(side=tk.RIGHT)

	# Temp path
	row2 = tk.Frame(frame)
	row2.pack(fill=tk.X, pady=12)
	tk.Label(row2, text="Temp path:", width=10, anchor='e').pack(side=tk.LEFT)
	output_var = tk.StringVar(value="D:/abc")
	output_entry = tk.Entry(row2, textvariable=output_var, font=("Segoe UI", 11))
	output_entry.pack(side=tk.LEFT, fill=tk.X, expand=True, ipady=8, padx=(8, 8))

	# Output path
	row3 = tk.Frame(frame)
	row3.pack(fill=tk.X, pady=12)
	tk.Label(row3, text="Output path:", width=10, anchor='e').pack(side=tk.LEFT)
	output_var3= tk.StringVar(value="D:/coco12903")
	output_entry3 = tk.Entry(row3, textvariable=output_var3, font=("Segoe UI", 11))
	output_entry3.pack(side=tk.LEFT, fill=tk.X, expand=True, ipady=8, padx=(8, 8))

	# Submit button
	row4 = tk.Frame(frame)
	row4.pack(fill=tk.X, pady=24)
	submit_btn = tk.Button(row4, text="Submit", width=14, height=2,
						 command=lambda: on_submit(submit_btn))
	submit_btn.pack(pady=10)

	def on_submit(btn: tk.Button):
		input_path = input_var.get().strip()
		output_path = output_var.get().strip()
		output_path3 = output_var3.get().strip()
		if not input_path:
			messagebox.showwarning("Notice", "Please select an image folder")
			return
		if not Path(input_path).exists():
			messagebox.showerror("Error", "Image folder does not exist")
			return
		if not output_path:
			messagebox.showwarning("Notice", "Please provide an output path")
			return
		btn.config(state=tk.DISABLED, text="Processing...")
		run_pipeline_async(input_path, output_path, output_path3, btn)

	root.mainloop()


if __name__ == "__main__":
	create_ui() 